package sysman.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import org.springside.core.dao.HibernateEntityDao;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

import sysman.model.Backlog;

import common.PageInfo;

//待办事项类
public class BacklogDao extends HibernateEntityDao<Backlog>{
	public void save(Backlog po) {
		this.getHibernateTemplate().save(po);
	}
	
	public List<Backlog> findList(HashMap<String, String> hm, PageInfo pageInfo){
		try{
		StringBuffer hql = new StringBuffer();
		/*------------查询条件-------------*/
		//注意以设值方式组装查询条�?,参�?�PageItemManager 方法:findList
		
		String title = (String)hm.get("title");
		String status = (String)hm.get("status");
		String type = (String)hm.get("type");
		String sDate = (String)hm.get("sDate");
		String eDate = (String)hm.get("eDate");
		String userId = (String)hm.get("userId");

		hql.append("from Backlog po where (1=1) ");
		hql.append(" and (instr(','||po.acceMan||',',',"+userId+",')>0) ");	
		if (StringUtils.isNotBlank(title)) {
			hql.append(" and (po.title like '%"+title+"%')");
		}
		if (StringUtils.isNotBlank(status)) {
			if("20".equals(status)) //待阅
				hql.append(" and status='20' and ( instr(po.readtime,',"+userId+"&')=0 or po.readtime is null) ");
			else if("21".equals(status))//已阅
				hql.append(" and status='21' and ( instr(po.readtime,',"+userId+"&')>0) ");
			else
				hql.append(" and (po.status = " +status+ " )");
		}
		if (StringUtils.isNotBlank(type)) {
				hql.append(" and (po.type = " +type+ " )");
		}
		if(StringUtils.isNotBlank(sDate)){
			hql.append(" and (to_char(po.sendDate,'yyyy-MM-dd') > '" +sDate+ "' )");
		}
		
		if(StringUtils.isNotBlank(eDate)){
			hql.append(" and (to_char(po.sendDate,'yyyy-MM-dd') < '" +eDate+ "' )");
		}
		hql.append(" order by po.id desc");
		//分页查询-START
		if(pageInfo!=null)
		{
			String qHql = hql.toString();
			List countList = this.getHibernateTemplate().find(qHql.replace("from Backlog", "SELECT COUNT(*) FROM Backlog"));
			int totalSize = 0;//�ܼ�¼��
			if(countList.size() > 0){
				totalSize = Integer.valueOf(countList.get(0).toString());
				pageInfo.setTotalSize(totalSize);
			} else {
				pageInfo.setTotalSize(0);
			}
			
			Query query = this.createQuery(hql.toString());
			query.setMaxResults(pageInfo.getPageSize());//设置查询数量
			query.setFirstResult(pageInfo.getPageNO() * pageInfo.getPageSize());//设置查询分段
			//分页查询-END
			return query.list();
		}
		   return this.find(hql.toString());
		}catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Backlog>();
		}
	}
	
	//待阅
	public List<Backlog> findlogReList(String userId){
		try{
			StringBuffer hql = new StringBuffer();
			hql.append("from Backlog po where po.type = 0 ");
			hql.append(" and (instr(','||po.acceMan||',',',"+userId+",')>0) ");
			hql.append(" and ( instr(po.readtime,',"+userId+"&')=0 or po.readtime is null) ");
			hql.append(" order by po.id desc");
			//首页查询-5条记录返回
			Query query = this.createQuery(hql.toString());
			query.setMaxResults(5);//设置查询数量
			query.setFirstResult(0);//设置查询分段
			return query.list();
			}catch (Exception e) {
				e.printStackTrace();
				return new ArrayList<Backlog>();
			}
	}
	
	//待办
	public List<Backlog> findlogDoList(String userId){
		try{
			StringBuffer hql = new StringBuffer();
			hql.append("from Backlog po where po.type = 1 and po.status = '10'");
			hql.append(" and (po.acceMan = '"+userId+"') ");
			hql.append(" order by po.id desc");
			//首页查询-5条记录返回
			Query query = this.createQuery(hql.toString());
			query.setMaxResults(5);//设置查询数量
			query.setFirstResult(0);//设置查询分段
			return query.list();
			}catch (Exception e) {
				e.printStackTrace();
				return new ArrayList<Backlog>();
			}
	}
	
	
	public String updateMsg(String id, String userId, String userName, String type)
	{
		String msg = "";
        try {
			Date date = new java.util.Date();
			SimpleDateFormat Timeformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	String s = "," + userId + "&" + userName + "&" + Timeformat.format(date).toString();
    		StringBuffer hql = new StringBuffer();
    		if (type.equals("1"))//阅读
    		{
    			hql.append("update BACKLOG set status='21', readtime=readtime||'"+s+"' where (id = '"+id+"') and (instr(readtime,',"+userId+"&')=0 or readtime is null)");
    		}
    		else //办理
    		{
    			hql.append("update BACKLOG set status='11', deltime=deltime||'"+s+"' where (id = '"+id+"') and (instr(deltime,',"+userId+"&')=0 or deltime is null)");
    		}
			Session sess=this.getSession();
			SQLQuery sQLQuery=sess.createSQLQuery(hql.toString());
			sQLQuery.executeUpdate(); 
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return msg;
	}
	
	public void updateMsg(String id){
		String hql = " update Backlog set status='11' where id="+id;
		this.getSession().createQuery(hql).executeUpdate();
	}
	
	public boolean deleteByIds(String ids){
		boolean isSuccessful = false;
		try{			
			String hql = "DELETE FROM Backlog po WHERE po.id IN (" + ids + ")";
			this.getHibernateTemplate().bulkUpdate(hql);
			isSuccessful = true;
		} catch (Exception e){
			e.printStackTrace();
		}
		
		return isSuccessful;
	}
}
